const { app, protocol, BrowserWindow } = require('electron')
const path = require('path')

let win

app.whenReady().then(() => {
  console.log('ready', protocol.getStandardSchemes())
  win = new BrowserWindow({ width: 800, height: 600 })
  win.loadFile(path.join(__dirname, '../renderer/index.html'))
  win.webContents.openDevTools({ mode: 'bottom' })
  interceptStringProtocol()
  registerHttpProtocol()
})

// 以 string 方式拦截协议
function interceptStringProtocol() {
  protocol.interceptStringProtocol('http', (request, callback) => {
    callback({
      mimeType: 'application/json',
      data: JSON.stringify({ name: 'keliq' }),
    })
  })
}

// 注册文件协议
function registerHttpProtocol() {
  protocol.registerHttpProtocol('hello', (request, callback) => {
    const relativePath = request.url.split('/').pop()
    callback({
      url: `https://img.zlib.cn/${relativePath}`,
      method: 'GET',
    })
  })
}
